package cn.hs.ph.servlet;

import cn.hs.ph.dao.impl.userDaoImpl;
import cn.hs.ph.dao.userDao;
import cn.hs.ph.entity.User;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        userDao userDao = new userDaoImpl();
        User user = userDao.selectUsernameAndPwd(username,password);

        if (user==null){
            Result error = Result.error("账号或密码错误");
            resp.getWriter().write(JSON.toJSONString(error));
        }else {
            Result success = Result.success("登陆成功！");
            resp.getWriter().write(JSON.toJSONString(success));
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
